<?php
	function IsWX_browerInfo()
	{
		if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') === false)
		{
	    	return false;
		} 
		else 
		{
			return true;
		}
	}
	
	function adminpermission($one,$two='')
	{
		$loginadmin =  $_SESSION['loginadmin'];
		if(!empty($loginadmin))
		{
			if($loginadmin['id'] == 1)
			{
				return true;
			}
			else
			{
				$permissionStr =  getLoginAdminInfo('permission');
				
				
				
				if(!empty($permissionStr))
				{
					$permission = json_decode($permissionStr,true);
					
					
					if(!empty($one) && !empty($two))
					{
						return in_array($two, $permission[$one]);
						
					}
					else
					{
						return !empty($permission[$one]);
					}
				}
			}
		}
		else
		{
			echo "<script>
				function logout()
				{
					var url_logout = '".SITE_URL."admin.php?a=login';
					try
					{
						window.parent.location = url_logout;
					}
					catch(e)
					{
						alert(e.message);
					}
				}
				logout();
				</script>";
			exit;
		}
	}
	
	

	function getClientIP()
	{
		$ip = $_SERVER['REMOTE_ADDR'];
		
		if($ip)
		{
			return ip2long($ip);
			
		}
		else
		{
			return 0;
		}

	}
	
	function requestArrayParm($request,$filter=0)
	{
		$parm = array();
		foreach ($request as $key => $value)
		{
			if(!empty($filter))
			{
				if(!empty($value))
				{
					if(is_array($value))
					{
						$parm[$key] = $value;
					}
					else
					{
						$parm[$key] = trim($value);
					}
				}
			}
			else
			{
				if(is_array($value))
				{
					$parm[$key] = $value;
				}
				else
				{
					$parm[$key] = trim($value);
				}
			}
		}
		return $parm;
	}
	
	function saveOpLogInfo($table,$id,$msg)
	{
		unset($data);
		$data['table'] = $table;
		$data['uid'] = getLoginAdminInfo('id');
		$data['opdesc'] = $msg;
		$data['union_id'] = $id;
		$data['optime'] = time();
		_OpLog::addOpLogInfo($data);
	}
	
	function getLoginAdminInfo($model='')
	{
		$loginadmin = $_SESSION['loginadmin'];
		if($model)
		{
			return $loginadmin[$model];
		}
		else
		{
			return $loginadmin;
		}
		
	}
	
	/**
	 * 获取参数
	 * @param $name 参数名称
	 * @param $type 来源
	 * @param $default 默认值
	 */
	function getParam($name,$type='get',$default=''){
		if(!empty($name)){
			$type = strtolower($type);
			switch ($type) {
				case 'get':
					$name = isset($_GET[$name])?$_GET[$name]:$default;
					break;
				case 'post':
					$name = isset($_POST[$name])?$_POST[$name]:$default;
					break;
				case 'request':
					if(isset($_POST[$name])){
						$name = $_POST[$name];
					}elseif(isset($_GET[$name])){
						$name = $_GET[$name];
					}else{
						$name = $default;
					}
					break;
				default:
					$name = isset($_GET[$name])?$_GET[$name]:$default;
				break;
			}
			if( $name !== 0 && $name !== '0' && empty($name) ){
				$name = $default;
			}
			if(is_string($name)){
				$name = trim($name);
			}
			return $name;
		}
		return false;
	}
	
	/**
	 * 记录日志
	 * @param unknown $content
	 * @param unknown $type
	 */
	function fileLog($content,$type='debug'){
		require_once (ROOTDIR."source/service/Logger.class.php");
		global $_logfile, $_logconf, $_loglevel;
		$type = strtolower($type);
		switch ($type) {
			case 'db':
				$logfile = $_logfile['db'];
				$tag = 'db';
				if($_SESSION['loginuser']['name']){
					$content .= '|'.$_SESSION['loginuser']['name'].'|'.date("Y-m-d H:i:s");
				}else{}
				
				Logger::instance('file', $logfile, $tag, $_logconf, $_loglevel)->info($content);
			break;
			case 'info':
				$logfile = $_logfile['debug'];
				$tag = 'info';
				Logger::instance('file', $logfile, $tag, $_logconf, $_loglevel)->info($content);
			break;
			case 'debug':
	
			default:
				$logfile = $_logfile['debug'];
				$tag = 'debug';
				Logger::instance('file', $logfile, $tag, $_logconf, $_loglevel)->debug($content);
			break;
		}
	}
	
	function getcode($length)
	{//生成php随机数
		$pattern = '1234567890 ';//字符池
		for($i=0;$i<$length;$i++) {
			$key .= $pattern{mt_rand(0,9)};
		}
		return $key;
	}

	function echoJSON($res)
	{
		header('Content-Type: application/json; charset=utf-8');
		echo json_encode($res);
		exit;
	}
	
	function setUserInfo($userinfo)
	{
		$_SESSION['loginkey'] = $userinfo['loginkey'];
	}
	
	function getUserInfo($falid='model')
	{
		$userinfo = getUserInfoByCookie();
		
		if($falid == 'model')
		{
			return $userinfo;
		}
		else
		{
			return $userinfo[$falid];
		}
	}
	
	function getUserInfoByCookie()
	{
		
		if(!empty($_SESSION['loginkey']))
		{
			unset($query);
			$query['loginkey'] = $_SESSION['loginkey'];
			
			$query['status'] = '1';
			
			
			return _User::getUserInfo($query);
		}
	
	}
	
	function chuliPhone($mobile)
	{
		return substr_replace($mobile, '****', 3, 4);
	}
	
	function sendMsg($info)
	{
		$url = 'http://www.zjysms.com/send/gsend.asp';
		
		$user = 'fumengyzm';
		
		$pwd = 'fumeng8815';
		
		//$content = iconv('utf-8', 'gb2312', $info['msg']);
		$content = mb_convert_encoding($info['msg'],'gb2312','utf-8');
		
		if(empty($content))
		{
			$content = $info['msg'];
		}
		
		
		
		$phone = $info['phone'];
		
		$sendurl = '?txt=ccdx&name='.$user.'&pwd='.$pwd.'&dst='.$phone.'&msg='.$content;

		$return = sock_post($url,$sendurl);
		
		$rrr['return'] = $return;
		$rrr['msg1'] = $content;
		$rrr['msg2'] = $info['msg'];
		fileLog(json_encode($rrr));
		return $rrr;
	}
	
	function sock_post($url,$query,$post=true,$timeout=5)
	{
		$data = "";
		$info = parse_url($url);
		$fp = fsockopen($info["host"],80,$errno,$errstr,$timeout);
		if(!$fp)
		{
			return $data;
		}
		$head="POST ".$info['path']." HTTP/1.0\r\n";
		$head.="Host: ".$info['host']."\r\n";
		$head.="Referer: http://".$info['host'].$info['path']."\r\n";
		$head.="Content-type: application/x-www-form-urlencoded\r\n";
		$head.="Content-Length: ".strlen(trim($query))."\r\n";
		$head.="\r\n";
		$head.=trim($query);
		$write=fputs($fp,$head);
		$header = "";
		while ($str = trim(fgets($fp,4096))) {
			$header.=$str;
		}
		while (!feof($fp)) {
			$data .= fgets($fp,4096);
		}
		return $data;
	}
	
	function curl_send_post($url,$query=array(),$post=false,$timeout=5)
	{
		try
		{

			$ch = curl_init();
			curl_setopt ( $ch, CURLOPT_URL, $url );
			curl_setopt ( $ch, CURLOPT_POST, $post );//post方式
			curl_setopt ( $ch, CURLOPT_HEADER, 0 );
			curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
			
			if(!empty($query))
			{
				curl_setopt ( $ch, CURLOPT_POSTFIELDS, $query );
			}
	
			curl_setopt ( $ch, CURLOPT_TIMEOUT, $timeout );
			
			//执行
			$return = curl_exec ( $ch );
			//释放
			curl_close ( $ch );
		}
		catch(Exception $e)
		{
		//	print_r($e->getMessage());

		}

		return $return;
	}
	
	function adminlogincheck()
	{	
		$loginadmin = $_SESSION['loginadmin'];
		
		if(empty($loginadmin))
		{
			echo "<script>
				function logout()
				{
					var url_logout = '".SITE_URL."admin.php?a=login';
					try
					{
						window.parent.location = url_logout;
					}
					catch(e)
					{
						alert(e.message);
					}
				}
				logout();
				</script>";
			exit;
		}
		else
		{
			return $loginadmin;
		}
	}
	
	function set_URL_Param($data)
	{
		foreach ($data as $key => $value)
		{
			$parm[] = $key.'='.$value;
		}
		
		return join('&', $parm);;
	}
	
	function get_URL_Param()
	{
		$str = $_SERVER["QUERY_STRING"];
		$arr = explode('&', $str);
		
		foreach ($arr as $key => $value)
		{
			$arr1 = explode('=', $value);
			if($arr1[0] != 'page')
			{
				$data[$arr1[0]] = $arr1[1];
			}
		}
		
		return $data;
	}
	
	function time_formart($time,$type=1)
	{
		if(empty($time))
		{
			return "";
		}
		
		if($type == 1)
		{
			return date("Y-m-d",$time);
		}
		
		if($type == 2)
		{
			return date("Y-m-d H:i",$time);
		}
		
		if($type == 3)
		{
			return date("H:i",$time);
		}
		
		if($type == 4)
		{
			return date("Y-m-d H:i:s",$time);
		}
	}
	
	/**
	 * 分页条
	 * @param string $url 页面地址
	 * @param intval $page 当前页码
	 * @param intval $total 总页数
	 * @param intval $tab 显示页面标签个数
	 */
	function pagenav($url,$page,$total)
	{
		
		$tab=7;
		if($total<1)
		{
			$total = 1;
		}	
		
		if($page<1)
		{
			$total = 1;
		}
			
		if($total <= $tab){
			$_start = 1;
			$_end = $total;
		}elseif($page <= ceil($tab/2)){
			$_start = 1;
			$_end = $tab;
		}elseif($page >= $total - $tab/2){
			$_start = $total - ($tab -1);
			$_end = $total ;
		}else{
			$_start = $page - ceil($tab/2) +1 ;
			$_end = $page + ceil($tab/2) -1;
		}
		$_page = $page;
		$_prepage = ($page<=1)?1:($page-1);
		$_nextpage = ($page>=$total)?$total:($page+1);
		$_url = $url;
		$_totalpage = $total;
		include renderTemplate('admin/page');
	}
